package com.northpool.operator.common.test;

import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

@OperatorInfo(
        ref = "SleepOperator",
        name = "延迟执行",
        description = "延迟执行"
)
public class SleepOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "sleepSecond", name = "休眠时间", desc = "休眠时间，单位秒, 不能超过600秒", type = TypeEnum.NUMBER, required = true)
    })
    public SleepOperator() {
    }

    public static void main(String[] args) throws Exception {
        OperatorTestBuild.execute(SleepOperator.class, TestParamBuilder.aBuilder()
                .param("sleepSecond", "10").build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        BigDecimal sleepSecond = paramTool.getNumber("sleepSecond");
        long time = sleepSecond.longValue();
        if (time > 600) {
            this.log("休眠时间超过600秒，按600秒处理");
            time = 600;
        }
        this.log("休眠开始: {}秒", time);
        TimeUnit.SECONDS.sleep(time);
        this.log("休眠结束");
    }
}
